/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstround2009;

import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author robit
 */
public class SquareMath{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C

    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        return;
    }
    static void refreshCache(int v,String s,boolean[] open, String[] cache){
        if(0<=v&&v<=MAXQUERY&&open[v]){
            if(cache[v]==null || s.compareTo(cache[v])<0)
            cache[v]=s;
        }
    }
    static int refreshAnswer(int que[], boolean[] open, String[] cache, String[]ans){
        int done=0;
        for(int i=0;i<251;i++){
            if(open[i]&&cache[i]!=null){
                open[i]=false;
                done++;
                //sys.println(cache[i]);
            }
        }
        return done;
    }
    
    final static int MAXQUERY=250;
    static int Q;
    final static int [] dr={0,-1,0,1};
    final static int [] dc={1,0,-1,0};
    /*
      calculate the answer
    */
    private static void reckon(int line) {
        int W=sc.nextInt();
        Q=sc.nextInt();
        char sqr[][]=new char[W][W];
        for(int i=0;i<W;i++){
            String tmp=sc.next();
            for(int j=0;j<W;j++){
                sqr[i][j]=tmp.charAt(j);
            }
        }

        int que[]=new int[Q];
        boolean closed[]=new boolean[Q];
        String ans[]=new String[Q];
        for(int i=0;i<Q;i++){
            que[i]=sc.nextInt();
        }

        //sys.print(open[250]);
        int done=0;
        HashMap<Integer, String>[][] expr=new HashMap[W][W];
        for(int r=0;r<W;r++)
        for(int c=0;c<W;c++)
        {
            if(sqr[r][c]<'0'||sqr[r][c]>'9')continue;
            expr[r][c]=new HashMap<Integer, String>();
            int v=sqr[r][c]-'0';String s =""+sqr[r][c];
            expr[r][c].put(v,s);
            for(int i=0;i<Q;i++){
                if(v==que[i]){
                    if(!closed[i]){
                        ans[i]=s;
                        closed[i]=true;
                        done++;
                    }                    
                }
            }
            //refreshCache(v, s, order, cache);
        }
        //done+=refreshAnswer(que, order, cache, ans);
        int count=0;
        // while not all answers are found yet, previous length is l-1
        HashSet<Integer>[][] was = new HashSet[W][W];
         for (int i = 0; i < W; i++) {
          for (int j = 0; j < W; j++) {
                was[i][j] = new HashSet<Integer>();
          }
         }
        while(done<Q){// to be mod
            // this loop will be concerned with the Strings of lenghth l
            HashMap<Integer, String>[][] buffer=new HashMap[W][W];
            for(int r=0;r<W;r++)
            for(int c=0;c<W;c++)
            buffer[r][c]=new HashMap<Integer, String>();// to store exprs of the next length
        
            // for each vertex v
            for(int r=0;r<W;r++)
            for(int c=0;c<W;c++){ 
                if(sqr[r][c]<'0'||sqr[r][c]>'9')continue;
                for(Map.Entry <Integer,String> e:expr[r][c].entrySet()){
                was[r][c].add(e.getKey());
               // for each vertex u connected to v
               for(int d=0;d<4;d++){
                   int rr=r+dr[d],cc=c+dc[d];
                   if(rr<0||rr>=W||cc<0||cc>=W)continue;
                   int sig=sqr[rr][cc]=='+'?1:-1;
                   char op=sqr[rr][cc];
                   for(int dd=0;dd<4;dd++){
                        int rrr=rr+dr[dd],ccc=cc+dc[dd];
                        if(rrr<0||rrr>=W||ccc<0||ccc>=W)continue;
                        // Offers all v's strings of length l-1 to u
                        // Also, refreshCache the answer if neccesary                        
                            int v=e.getKey()+sig*(sqr[rrr][ccc]-'0');
                            if(was[rrr][ccc].contains(v))continue;
                            String s=e.getValue()+op+sqr[rrr][ccc];
                            if(!buffer[rrr][ccc].containsKey(v)){
                                buffer[rrr][ccc].put(v, s);
                                //refreshCache(v, s, order, cache);
                            }else {
                                String olds=buffer[rrr][ccc].get(v);
                                if(s.compareTo(olds)<0){
                                    buffer[rrr][ccc].put(v, s);
                                    //refreshCache(v, s, order, cache);
                                }
                            }

                            for(int i=0;i<Q;i++){
                                if(v==que[i]&&!closed[i]){
                                    if(ans[i]==null || s.compareTo(ans[i])<0)
                                    ans[i]=s;
                                }
                            }
                        }
                    }
                }
            }
            for(int i=0;i<Q;i++){
                if(!closed[i]&&ans[i]!=null){
                    closed[i]=true;
                    done++;
                }
            }
            //done+=refreshAnswer(que, order, cache, ans);
            //sys.println(String.format("done=%d, Q=%d",done,Q));
            //sys.println(count++);
            expr=buffer;
        }

        out.println(String.format("Case #%d:",line));
        for(int i=0;i<Q;i++)
            out.println(ans[i]);
        //anl.println(String.format("N=%d K=%d\n%s", N, K, ans));
        //println(output);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        //preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="C";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            initFileIO(filename);
            int lineNums=sc.nextInt();
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
                sys.println(i);
            }
        } finally {
            endIO();
        }
    }

    private static Scanner sc = null;
    private static PrintWriter out,anl = null;
    private static PrintStream sys=System.out;
    public final static int OUTFILE=0,ANALFILE=1;

    public static void  initFileIO(String filename) throws IOException{
        sc = new Scanner(
                new BufferedReader(new FileReader(filename+".in")));
            //s.useLocale(Locale.US);
        out = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".out")));
        anl = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".anal")));
        
    }

    public static void endIO(){
        sc.close();
        out.close();
    }

}
